//
//  RestPasswordViewController.m
//  MangoShangJia
//
//  Created by Macbook on 25/7/17.
//  Copyright © 2017年 dazhou. All rights reserved.
//

#import "RestPasswordViewController.h"
#import "LoginController.h"

@interface RestPasswordViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *PassTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *AgainPassTextFiled;
@property (weak, nonatomic) IBOutlet UIButton *SaveBut;

@end

@implementation RestPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.title = @"忘记密码";
    _PassTextFiled.delegate = self;
    _AgainPassTextFiled.delegate = self;
    
    [_PassTextFiled addTarget:self action:@selector(ChangeText:) forControlEvents:UIControlEventEditingChanged];
    [_AgainPassTextFiled addTarget:self action:@selector(ChangeText:) forControlEvents:UIControlEventEditingChanged];
}

- (void)ChangeText:(UITextField *)sender {
    
    if (_PassTextFiled.text.length>=6&&_AgainPassTextFiled.text.length>=6) {
        
        _SaveBut.backgroundColor = UIColorFromRGBA(0x3cb83a, 1);
        
        _SaveBut.userInteractionEnabled = YES;
        
    } else if (_PassTextFiled.text.length<6||_AgainPassTextFiled.text.length<6) {
        
        _SaveBut.backgroundColor = UIColorFromRGBA(0xdfdfdf, 1);
        
        _SaveBut.userInteractionEnabled = NO;
        
    }
    
}
- (IBAction)SaveBut:(id)sender {
    
    
    if ([self checkRightAll] == NO) {
        [self.view makeToast:@"请输入正确的内容"];
    }
    LoginController* longinVC = [[LoginController alloc]init];
    [self.navigationController pushViewController:longinVC animated:YES];
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    manger.responseSerializer = [AFJSONResponseSerializer serializer];
    manger.requestSerializer = [AFJSONRequestSerializer serializer];
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/ttml", nil];
    NSString *token = [USERDEFAULT objectForKey:@"token"];
    
    NSString *Url = [NSString stringWithFormat:@"%@%@/?token=%@",ServiceURL,req_change,token];
    NSDictionary *params = @{@"mobile":_MobStr,@"password":_AgainPassTextFiled.text};
    
    
    
    [manger POST:Url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *code = responseObject[@"code"];
        if ([code isEqualToString:@"0"]) {
            [self performSelectorOnMainThread:@selector(changeYes) withObject:nil waitUntilDone:YES];
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];

}

-(void)changeYes{
    //pop到登陆页面
    int index = (int)[[self.navigationController viewControllers]indexOfObject:self];
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:(index -2)] animated:YES];
    

}
- (BOOL)checkRightAll
{
    if (_AgainPassTextFiled.text.length > 0 && _PassTextFiled.text.length > 0 &&[_PassTextFiled.text isEqualToString:_AgainPassTextFiled.text]) {
        return YES;
    }
    
    return NO;
}
@end
